package com.rt.easyjava.base.biz1.service.impl.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rt.easyjava.base.biz1.bean.TmptAuthUserBiz;
import com.rt.easyjava.base.biz1.dto.TmptAuthUserBizDto;
import com.rt.easyjava.base.biz1.mapper.TmptAuthUserBizMapper;
import com.rt.easyjava.base.biz1.service.intf.base.TmptAuthUserService;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.config.dozer.MyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;

/**
 *  服务实现类
 * @author wuxiaoyong
 * @since 2020-09-18
 */
@Slf4j
public class TmptAuthUserServiceImpl extends ServiceImpl<TmptAuthUserBizMapper, TmptAuthUserBiz> implements TmptAuthUserService {
    @Autowired
    protected MyMapper mapper;
    @Resource
    protected TmptAuthUserBizMapper tmptAuthUserBizMapper;

    // ==================================================================================
    // ========== 以下使用的是 Mybatis-plus 实现, 适合单表操作 ==============================
    // ==================================================================================

    /**
    * create a object
    * @param tmptAuthUserBizDto
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> create(TmptAuthUserBizDto tmptAuthUserBizDto) {
        Result<TmptAuthUserBizDto> result = new Result<>();

        // dto to entity
        TmptAuthUserBiz tmptAuthUserBiz = mapper.map(tmptAuthUserBizDto, TmptAuthUserBiz.class);

        tmptAuthUserBizMapper.insert(tmptAuthUserBiz);

        return result.success();
    }

    /**
    * update a object by Id
    * @param tmptAuthUserBizDto
    * @param updateWrapper
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> update(TmptAuthUserBizDto tmptAuthUserBizDto, UpdateWrapper updateWrapper) {
        Result<TmptAuthUserBizDto> result = new Result<>();

        // dto to entity
        TmptAuthUserBiz tmptAuthUserBiz = mapper.map(tmptAuthUserBizDto, TmptAuthUserBiz.class);

        tmptAuthUserBizMapper.update(tmptAuthUserBiz, updateWrapper);

        return result.success();
    }

    /**
    * 分页 (无合计功能)
    * @param page
    * @param queryWrapper
    * @return
    */
    public Result<TmptAuthUserBizDto> page(Page<TmptAuthUserBiz> page, QueryWrapper<TmptAuthUserBiz> queryWrapper){
        Result<TmptAuthUserBizDto> result = new Result<>();

        // mapper
        IPage<TmptAuthUserBiz> iPage = tmptAuthUserBizMapper.selectPage(page, queryWrapper);

        // 查询结果转换成DTO
        List<TmptAuthUserBizDto> tmptAuthUserBizDtoList= mapper.maplist(iPage.getRecords(), TmptAuthUserBizDto.class);

        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthUserBizDtoList);

        return result.success();
    }

    /**
    * select all by queryWrapper
    * @param queryWrapper
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> selectBy(QueryWrapper<TmptAuthUserBiz> queryWrapper) {
        Result<TmptAuthUserBizDto> result = new Result<>();

        List<TmptAuthUserBiz> list = tmptAuthUserBizMapper.selectList(queryWrapper);

        List<TmptAuthUserBizDto> dtoList = mapper.maplist(list, TmptAuthUserBizDto.class);

        return result.setList(dtoList).success();
    }

    /**
    * select one by id
    * @param id
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> selectById(Long id) {
        Result<TmptAuthUserBizDto> result = new Result<>();

        TmptAuthUserBiz tmptAuthUserBiz = tmptAuthUserBizMapper.selectById(id);

        TmptAuthUserBizDto tmptAuthUserBizDto = mapper.map(tmptAuthUserBiz, TmptAuthUserBizDto.class);

        return result.setObj(tmptAuthUserBizDto).success();
    }



    /**
    * select dto list by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> selectDtoListByKey(String columnName, String key) {
        Result<TmptAuthUserBizDto> result = new Result<>();

        List<TmptAuthUserBiz> tmptAuthUserBizList = selectListByKey(columnName, key);
        if (tmptAuthUserBizList == null) {
            return result.fault("no data by key:"+key);
        }

        List<TmptAuthUserBizDto> tmptAuthUserBizDtoList = mapper.maplist(tmptAuthUserBizList, TmptAuthUserBizDto.class);

        return result.setList(tmptAuthUserBizDtoList).success();
    }

    /**
    * select bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public TmptAuthUserBiz selectByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptAuthUserBiz> eventRuleBizList = tmptAuthUserBizMapper.selectList(queryWrapper);
        if (eventRuleBizList.size() == 0) {
            return null;
        }
        return eventRuleBizList.get(0);
    }

    /**
    * select list bean by key
    * @param columnName
    * @param key
    * @return
    */
    @Override
    public List<TmptAuthUserBiz> selectListByKey(String columnName, String key) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(columnName, key);
        queryWrapper.eq("deleteflag", 0);
        List<TmptAuthUserBiz> eventRuleBizList = tmptAuthUserBizMapper.selectList(queryWrapper);
        return eventRuleBizList;
    }






    // ==================================================================================
    // ========== 以下使用的是自定义的 mapper.xml 实现, 适合多表查询 =========================
    // ==================================================================================

    /**
    * 分页查询：mybatis plus 自定义sql分页查询
    * 默认开启合计查询，按需注掉代码
    * @param page
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> myPage(Page<TmptAuthUserBiz> page, QueryWrapper<TmptAuthUserBiz> queryWrapper) {
        Result<TmptAuthUserBizDto> result = new Result();

        IPage<TmptAuthUserBiz> iPage = tmptAuthUserBizMapper.myPage(page, queryWrapper);
        // 查询结果转换成DTO
        List<TmptAuthUserBizDto> tmptAuthUserBizDtoList = mapper.maplist(iPage.getRecords(), TmptAuthUserBizDto.class);
        // 返回结果集
        result.getMap().put("total", iPage.getTotal());
        result.getMap().put("rows", tmptAuthUserBizDtoList);

        // 合计
        List<TmptAuthUserBiz> sumList = tmptAuthUserBizMapper.mySelectSum(queryWrapper);
        List<TmptAuthUserBizDto> sumDtoList = mapper.maplist(sumList, TmptAuthUserBizDto.class);
        result.getMap().put("footer", sumDtoList);

        return result.success();
    }

    /**
    * 查询
    * @param queryWrapper: 一定要是这个代理工具类才行：QueryWrapper queryWrapper = MyQueryWrapper.buildQueryWrapper(...)
    * @return
    */
    @Override
    public Result<TmptAuthUserBizDto> mySelectBy(QueryWrapper<TmptAuthUserBiz> queryWrapper) {
        Result<TmptAuthUserBizDto> result = new Result();

        List<TmptAuthUserBiz> list = tmptAuthUserBizMapper.mySelectBy(queryWrapper);
        List<TmptAuthUserBizDto> dtoList = mapper.maplist(list, TmptAuthUserBizDto.class);
        result.setList(dtoList);

        return result.success();
    }

    @Override
    public Result<TmptAuthUserBizDto> mySelectById(long id) {
        Result<TmptAuthUserBizDto> result = new Result();

        TmptAuthUserBiz tmptAuthUserBiz = tmptAuthUserBizMapper.mySelectById(id);
        TmptAuthUserBizDto dto = mapper.map(tmptAuthUserBiz,TmptAuthUserBizDto.class);
        result.setObj(dto);

        return result.success();
    }


}
